package lion.dev.web.paging;

import lion.dev.lang.MapJ;

/**
 *
 * 无限行数的分页
 *
 * 未指定最大行数，没有最后一行
 *
 * @author lion
 * @email hl_0074@sina.com
 * @date 2014年5月23日
 */
public class UnlimitedPaging implements Paging {

	private int pageNumber;
	private int pageSize;

	public UnlimitedPaging() {

	}

	public UnlimitedPaging(int page, int size) {

		this.pageNumber = page;
		this.pageSize = size;
	}

	@Override
	public void setPageNumber(int pageNumber) {

		this.pageNumber = pageNumber;
	}

	@Override
	public void setPageSize(int pageSize) {

		this.pageSize = pageSize;
	}

	@Override
	public int getStartRowNumber() {

		return (this.pageNumber - 1) * this.pageSize + 1;
	}

	@Override
	public int getEndRowNumber() {

		return this.pageNumber * this.pageSize;
	}

	@Override
	public int getPageNumber() {

		return this.pageNumber;
	}

	@Override
	public int getPageCount() {

		return -1;
	}

	@Override
	public boolean hasPreviousPaging() {

		if (this.pageNumber <= 1) { return false; }
		return true;
	}

	@Override
	public boolean hasNextPaging() {

		return true;
	}

	@Override
	public Paging getNextPaging() {

		UnlimitedPaging sp = new UnlimitedPaging();
		sp.setPageNumber(this.pageNumber + 1);
		sp.setPageSize(this.pageSize);

		return sp;
	}

	@Override
	public Paging getPreviousPaging() {

		if (this.pageNumber <= 1) { return null; }

		UnlimitedPaging sp = new UnlimitedPaging();
		sp.setPageNumber(this.pageNumber - 1);
		sp.setPageSize(this.pageSize);

		return sp;
	}

	@Override
	public Paging getFirstPaging() {

		UnlimitedPaging sp = new UnlimitedPaging();
		sp.setPageNumber(1);
		sp.setPageSize(this.pageSize);
		return null;
	}

	@Override
	public Paging getEndPaging() {

		return null;
	}

	@Override
	public int getMaxRowNumber() {

		return -1;
	}

	@Override
	public int getPageSize() {

		return this.pageSize;
	}

	@Override
	public MapJ map() {

		MapJ result = new MapJ();
		result.put("start", this.getStartRowNumber());
		result.put("pageSize", this.getPageSize());
		result.put("end", this.getEndRowNumber());
		result.put("page", this.getPageNumber());
		result.put("hasNext", this.hasNextPaging());
		result.put("hasPre", this.hasPreviousPaging());

		return result;
	}
}
